package com.qf.team2.netdisc.service.impl;

import com.qf.team2.netdisc.common.exception.BusinessException;
import com.qf.team2.netdisc.common.pojo.entity.Document;
import com.qf.team2.netdisc.common.pojo.qo.DocumentQO;
import com.qf.team2.netdisc.dao.DocumentInsertMapper;
import com.qf.team2.netdisc.service.DocumentInsertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * 新建文件夹服务接口实现类
 *
 * @author Simon_Wong
 * @date 2021/05/26 10:49
 */
@Service
public class DocumentInsertServiceImpl implements DocumentInsertService {
    @Autowired
    private DocumentInsertMapper documentInsertMapper;

    @Override
    public Integer documentInsert(String username, Integer userId) {
        System.out.println("documentInsert");

        String files = "D://bf-netdisc/" + username;
        System.out.println(files);
        File file = new File(files);
        file.mkdir();
        DocumentQO documentQO = new DocumentQO();
        documentQO.setFileName(username);
        documentQO.setUserId(userId);
        documentQO.setFileSize(0);
        documentInsertMapper.insertFile(documentQO);
        String name = username + "\\\\";
        Document dbDocument = documentInsertMapper.selectDocumentByName(name, userId);
        Integer fileId = dbDocument.getId();
        if (fileId == null) {
            throw new BusinessException("文件夹创建失败");
        }
        return fileId;
    }

    /**
     * 新建文件夹
     *
     * @param url
     * @param name
     * @param userId
     * @return
     */
    @Override
    public Integer insertNewFile(String url, String name, Integer userId) {
        String urls = url + name + "\\";
        urls = urls.replace("\\", "\\\\");
        documentInsertMapper.insertNewFile(urls, name, userId);
        Document document = documentInsertMapper.selectDocumentByName(urls, userId);
        Integer fileId = document.getId();
        if (fileId == null) {
            throw new BusinessException("文件夹创建失败");
        }
        return fileId;

    }

}
